<?php
require_once ('library/Model/BaseModel.php');
/**
 *
 * @author Nguyen
 *        
 */
class Ticket extends BaseModel implements IModel {
	/**
	 *
	 * @var Trip
	 */
	private $trip;
	public function getId() {
		return array (
				'id',
				'trip',
				'customer' 
		);
	}
	public function checkTicket($id) {
		$sql = 'select 	t.id,
						t.quantity as tripquantity,
						c.quantity as carquantity
				from trip as t
				left join car as c on t.car = c.id where t.id = ' . $id;
		$entity = $this->db->fetchRow ( $sql );
		if ($entity ['tripquantity'] < $entity ['carquantity']) {
			return true;
		} else
			return false;
	}
	/*
	 * (non-PHPdoc) @see BaseModel::update()
	 */
	public function update(array $entity) {
		// TODO Auto-generated method stub
		$id = $entity ['id'];
		$oldentity = $this->getbyid ( array (
				'id' => $id 
		) );
		$this->trip = new Trip();
		$this->trip->downQuantity ( $oldentity ['trip'] );
		$this->trip->upQuantity ( $entity ['trip'] );
		$entity = iArray::removeAtTop ( $entity, 1 );
		$b = sizeof ( $entity );
		$c = 0;
		$sql = "update " . $this->getTable () . " set ";
		foreach ( $entity as $key => $value ) {
			$c += 1;
			if ($b == $c)
				$sql .= $key . " = " . "'" . $value . "'";
			else
				$sql .= $key . " = " . "'" . $value . "'" . ",";
		}
		$sql .= " where id = " . $id;
		echo $sql;
		return $this->db->execute ( $sql );
	}
	/*
	 * (non-PHPdoc) @see BaseModel::delete()
	 */
	public function delete(array $id) {
		// TODO Auto-generated method stub
		// Get list table relative
		$keyword = RelativeTable::listTable ( $this->getTable () );
		// Get entity of table muon xoa
		$entity = $this->getbyid ( $id );
		if (isset ( $keyword )) {
			// Vong lap cai table lien quan
			foreach ( $keyword as $key ) {
				$list = $this->getallbytable ( $key );
				// iDebug::debugArray($list);
				foreach ( $list as $en ) {
					if ($en [$this->getTable ()] == $entity ['id']) {
						return;
					}
				}
			}
		}
		// Lay chieu dai array
		$b = sizeof ( $id );
		// Dat bien flag
		$c = 0;
		// cau sql
		$sql = 'delete from ' . $this->getTable () . ' where ';
		foreach ( $id as $key => $value ) {
			$sql .= $key . '=' . $value;
			if ($c < $b - 1) {
				$sql .= ' and ';
				$c += 1;
			}
		}
		if ($this->db->execute ( $sql ) > 0) {
			$this->trip = new Trip ();
			$this->trip->downQuantity ( $entity ['trip'] );
		}
	}
	/* (non-PHPdoc)
	 * @see BaseModel::getall()
	 */
	public function getall($strlimit = NULL) {
		// TODO Auto-generated method stub
		$sql = 'select t.id,t.code,p1.name as pointstart,p2.name as pointend,c.username as customer,t.seat,t.status
				from ticket as t
				left join customer as c on t.customer = c.id
				left join trip as tr on t.trip = tr.id
				left join point as p1 on p1.route =tr.route and p1.position in (select min(p1.position) from point as p1 where  p1.route = tr.route)
				left join point as p2 on p2.route =tr.route and p2.position in (select max(p2.position) from point as p2 where  p2.route = tr.route) 
				order by t.id ASC '.$strlimit;
		return $this->db->fetchAll ( $sql );
	}
	public function getCarQuantity($tripid){
		$sql = 'select c.quantity
				from trip as t left join car as c on t.car = c.id where t.id = '.$tripid;
		return $this->db->fetchRow($sql);
	}
	/* (non-PHPdoc)
	 * @see BaseModel::generateCode()
	 */
	public function generateCode($key) {
		// TODO Auto-generated method stub
		$sql = 'select code from trip where id = '.$key;
		$code = $this->db->fetchRow($sql);
		$code = substr($code['code'], 0,3).'T';                       
		$xsql = 'select code from ticket where code like "'.$code.'%" order by code DESC limit 0,1';
		$x = $this->db->fetchRow($xsql);
		$x = intval(substr($x['code'], 4,strlen($x['code']))) + 1;
		if ($x < 10)
			return $code = $code.'0'.$x;
		return $code = $code.$x;
	}

	/* (non-PHPdoc)
	 * @see BaseModel::insert()
	 */
	public function insert(array $entity) {
		// TODO Auto-generated method stub
		$entity['code'] = $this->generateCode($entity['trip']);
		$sql = "insert into ticket (";
		$c = $b = 0;
		foreach ( $entity as $key => $value ) {
				
			if ($c == 0) {
				$sql .= $key;
				$c = 1;
			} else
				$sql .= "," . $key;
		}
		$sql .= ")";
		$sql .= " values (";
		foreach ( $entity as $key => $value ) {
			if ($b == 0) {
				$sql .= "'" . $value . "'";
				$b = 1;
			} else
				$sql .= "," . "'" . $value . "'";
		}
		$sql .= ")";
		return $this->db->execute ( $sql );
	}
}

?>